package com.app.miwawa.api.system.entity;

import java.util.Date;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.framework.common.api.entity.CommonEntityUUID;
import org.framework.common.api.support.model.ProcessModel;
import org.framework.common.ext.util.StringUtil;
import org.framework.common.ext.util.datasource.DynamicDataSourceHolder;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.jdbc.core.ColumnMapRowMapper;

import com.app.miwawa.api.system.entity.Easemob;
import com.easemob.server.example.utils.EasemobUtils;
import com.fasterxml.jackson.annotation.JsonFormat;


/**
 * <p>
 * 描述:</>
 * <p>Company:</>
 * @author zhaochong(赵冲)
 * @date 2016年3月4日下午1:14:01
 * @version 1.0.2016
 */

@Entity @Table(name = "TB_APP_USER")
public class AppUser extends CommonEntityUUID{

	/** serialVersionUID(long):. */
	private static final long serialVersionUID = -5106089592079145L;
	
	// 注册手机号
	@Column(name = "mobile", length = 50, nullable = true)
	private String mobile;
	
	// 邮箱
	@Column(name = "email", length = 50, nullable = true)
	private String email;
	
	// 登录密码
	@Column(name = "password", length = 50, nullable = true)
	private String password;
	
	// 用户图像
	@Column(name = "picture", length = 500, nullable = true)
	private String picture;
	
	// 昵称(注册后不能修改)
	@Column(name = "nick_name", length = 50, nullable = true)
	private String nickName;
	
	// 性别
	@Column(name = "sex", length = 5, nullable = true)
	private String sex;
	
	// 注册时间
	@Temporal(TemporalType.TIMESTAMP)
	@JsonFormat(pattern="yyyy-MM-dd HH:mm", timezone="GMT+8")
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Column(name = "create_time", nullable = false)
	private Date createTime;
	
	// 家长所选择的机构ID
	@Column(name = "shop_id", length = 50, nullable = true)
	private String shopId;
	
	// 家长所选择的机构名称
	@Column(name = "shop_name", length = 50, nullable = true)
	private String shopName;
	
	// 机构所属中心数据源
	@Column(name = "dbname", length = 50, nullable = true)
	private String dbname;
	
	/**
	 * @see org.framework.common.api.entity.CommonEntityUUID#save(org.framework.common.api.service.bo.CommonBoAware, org.springframework.jdbc.core.JdbcTemplate, java.lang.String[])
	 * @author chanlong(陈龙)
	 * @date 2016年8月30日 下午5:47:32
	 */
	@Override
	public Object save(final String... params) throws Exception {
		final AppUser user = new AppUser(sex, email, mobile, password, picture, nickName, dbname, shopId, shopName);
		final ProcessModel process = ProcessModel.factory();
		process.executor.execute(new Runnable() {
			@Override
			public void run() {
				try{
					// 切换数据源
					DynamicDataSourceHolder.setDataSource("default");
					
					// 新增APP账号
					String uid = process.bo.insert(user).getId(), eid = "C"+uid;
					
					// 注册家长APP环信
					if(!EasemobUtils.getIMUser(eid) && EasemobUtils.createIMUser(eid, password, nickName)){
						// 新增APP环信同步信息
						process.bo.insert(new Easemob(uid, eid, "家长", mobile, shopId));
					}
				}catch(Exception e){
					
				}
			}
		});
		return null;
	}

	/**
	 * 构造器
	 * 设置实体对象字段的默认值
	 */
	public AppUser() {
		this.status = "0";
		this.invalid = "F";
		this.createTime = new Date();
	}
	
	public AppUser(String dbname, String email, String mobile, String nickname, String relation) {
		this();
		this.email = email;
		this.mobile = mobile;
		this.dbname = dbname;
		this.nickName = nickname;
		this.password = StringUtil.password("000000");
		this.setSexAuto(relation);
		this.setShopinfo(dbname);
	}
	
	public AppUser(String sex, String email, String mobile, String password, String picture, String nickName, String dbname, String shopId, String shopName) {
		this();
		this.sex = sex;
		this.email = email;
		this.mobile = mobile;
		this.picture = picture;
		this.nickName = nickName;
		this.password = password;
		this.dbname = dbname;
		this.shopId = shopId;
		this.shopName = shopName;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public void setSexAuto(String relation) {
		switch (relation) {
			case "爸爸":
			case "爷爷":
			case "外公":
				this.sex = "男"; break;
			case "妈妈":
			case "奶奶":
			case "外婆":
				this.sex = "女"; break;
			default:
				this.sex = "女"; break;
		}
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public String getShopName() {
		return shopName;
	}

	public void setShopName(String shopName) {
		this.shopName = shopName;
	}

	public String getDbname() {
		return dbname;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}
	
	public void setShopinfo(final String dbname){
		String sql = "select id, name from miwawa.vb_app_shop where dbname = ? limit 1";
		Map<String, Object> shop = ProcessModel.factory().jdbc.queryForObject(sql, new ColumnMapRowMapper(), dbname);
		this.shopId = String.valueOf(shop.get("id")); 
		this.shopName = String.valueOf(shop.get("name")); 
	}
}
